package controllers

import (
	"animalss/cache"
	"animalss/models"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"time"
)

type AnimalsControllers struct {
	beego.Controller
}

func (c *AnimalsControllers) Add() {
	peoplename := c.GetString("paoplename")
	peopleddress := c.GetString("peopleddress")
	Peopleage, _ := c.GetInt("paopleage")
	an := models.Peoples{
		Pepoplename:  peoplename,
		Peopleddress: peopleddress,
		Peopleage:    Peopleage,
	}
	err := models.DB.Create(&an).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *AnimalsControllers) Upload() {
	petname := c.GetString("petname")
	img := c.GetString("img")
	address := c.GetString("address")
	an := models.Animals{
		Petname: petname,
		Img:     img,
		Address: address,
	}
	file, header, err := c.GetFile("img")
	defer file.Close()
	path := "static/img/" + header.Filename
	c.SaveToFile("img", path)
	if header.Size > 2*1024*1024 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "文件内存过大上传失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	//if filepath.Ext(header.Filename!=".jpg"){
	//	c.Data["json"] = Res{
	//		Code: 200,
	//		Msg:  "上传成功",
	//		Data: "127.0.0.1:8080" + path,
	//	}
	//
	//}
	c.ServeJSON()
	err = models.DB.Create(&an).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "上传成功",
			Data: "127.0.0.1:8080" + path,
		}
		c.ServeJSON()
	}
}
func (c *AnimalsControllers) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	query := models.DB.Model(&models.Animals{})
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	offset := (page - 1) * size
	var an []models.Animals
	key := "animals__show"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res != "" {
		json.Unmarshal([]byte(res), &an)
	} else {
		query.Limit(size).Offset(offset).Find(&an)
		jsonData, _ := json.Marshal(&an)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: an,
	}
	c.ServeJSON()
}
func (c *AnimalsControllers) Del() {
	id, _ := c.GetInt("id")
	var an []models.Animals
	err := models.DB.Where("id =?", id).Delete(&an).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *AnimalsControllers) Show() {
	id, _ := c.GetInt("id")
	var an models.Animals
	key := "animals_show"
	res, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if res != "" {
		json.Unmarshal([]byte(res), &an)
	} else {
		models.DB.Where("id =?", id).Find(&an)
		jsonData, _ := json.Marshal(&an)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "查询成功",
		Data: an,
	}
	c.ServeJSON()
}
